using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame1
{
  public class GameLogic : Microsoft.Xna.Framework.GameComponent
  {
   
    public GameLogic(Game game)
      : base(game)
    {
      //if (File.Exists("savegame.sav"))
      //{
      //  using (var stream = File.Open("savegame.sav", FileMode.Open))
      //  {
      //    var binary_formatter = new BinaryFormatter();
      //    ball = (Ball)binary_formatter.Deserialize(stream);
      //    loaded = true;
      //  }
      //}
      //else
      //{
      //  ball = new Ball();
      //}
      //game.Services.AddService(typeof(Ball), ball);
    }

    protected override void Dispose(bool disposing)
    {
      //Game.Services.RemoveService(typeof(Ball));

      base.Dispose(disposing);
    }

    //RenderingData rendering_data;
    public override void Initialize()
    {
      //if (!loaded)
      //{
      //  ball.position = new Vector2(0.0f, 0.0f);
      //  ball.velocity = new Vector2(10.0f, -150.0f);
      //}

      //rendering_data = (RenderingData)Game.Services.GetService(typeof(RenderingData));

      Game.Exiting += new EventHandler<EventArgs>(Game_Exiting);

      base.Initialize();
    }

    void Game_Exiting(object sender, EventArgs e)
    {
      //using (var stream = File.Open("savegame.sav", FileMode.Create))
      //{
      //  var binary_formatter = new BinaryFormatter();
      //  binary_formatter.Serialize(stream, ball);
      //}
    }

    public override void Update(GameTime gameTime)
    {
      var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

      //if(rendering_data == null)
      //  rendering_data = (RenderingData)Game.Services.GetService(typeof(RenderingData));

      //if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
        if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.B) == true)
         {
        while (Game.Components.Count > 0)
        {
          ((GameComponent)Game.Components[0]).Dispose();
        }

        Game.Components.Clear();

        Game.Components.Add(new Menu(Game));
      }


      ball.position = ball.position + ball.velocity * dt;
      ball.velocity = ball.velocity + Ball.G * dt;

      //var k1 = ball.Derivative();
      //var k2 = (ball + 0.5f * dt * k1).Derivative();
      //var k3 = (ball + 0.75f * dt * k2).Derivative();

      //ball = ball + (2.0f / 9.0f) * dt * k1 +
      //        (1.0f / 3.0f) * dt * k2 +
      //        (4.0f / 9.0f) * dt * k3;

      if (ball.position.Y > rendering_data.MaxY)
      {
        ball.position.Y = rendering_data.MaxY;
        ball.velocity.Y *= -0.8f;
      }

      //if (ball.position.X < 0.0f)
      //{
      //  ball.position.X = 0.0f;
      //  ball.velocity.X *= -1.0f;
      //}
      //if (ball.position.X > rendering_data.MaxX)
      //{
      //  ball.position.X = rendering_data.MaxX;
      //  ball.velocity.X *= -1.0f;
      //}
      //if (ball.position.Y < 0.0f)
      //{
      //  ball.position.Y = 0.0f;
      //  ball.velocity.Y *= -1.0f;
      //}
      //if (ball.position.Y > rendering_data.MaxY)
      //{
      //  ball.position.Y = rendering_data.MaxY;
      //  ball.velocity.Y *= -1.0f;
      //}

      //ball.velocity = ball.velocity + dt * 100.0f * GamePad.GetState(PlayerIndex.One).ThumbSticks.Left;

      base.Update(gameTime);
    }
  }
}
